package com.koron.css2.projectCenter.impl;

import com.koron.common.web.bean.PicAccessoryBean;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BaseAccessoryMetadataBean;
import com.koron.css2.baseConfig.bean.BaseReceiptAccessoryBean;
import com.koron.css2.baseConfig.mapper.BaseAccessoryMetadataMapper;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import com.koron.util.Upload;
import com.koron.util.encode.BASE64DecodedMultipartFile;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;



/**
 * 高拍仪图片上传
 *
 * @author: zhongxj
 * @date: 2020-07-13 17:20
 **/

public class PicUpload implements ServerInterface {

    private static Logger log = LoggerFactory.getLogger(PicUpload.class);


    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PicAccessoryBean picAccessoryBean = JsonUtils.objectToPojo(req.getData(), PicAccessoryBean.class);
            String data = picAccessoryBean.getData();
            String receiptId = picAccessoryBean.getReceiptId();
            String accessoryNo = picAccessoryBean.getAccessoryNo();
            String userNo = picAccessoryBean.getUserNo();
            String receiptType = picAccessoryBean.getReceiptType();
            StringBuilder sb = new StringBuilder();
            sb.append(data);
            MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(sb.toString());
            String path = Upload.upload(file);
            BaseAccessoryMetadataBean metadata = null;
            if (StringUtils.isNotBlank(path)) {
                metadata = addAccessoryData(factory,file,userInfo,path,receiptId,accessoryNo,userNo,receiptType);
            }
            MessageBean<BaseAccessoryMetadataBean> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "上传成功", BaseAccessoryMetadataBean.class);
            metadata.setAccessoryPath(ApplicationConfig.getAccessoryUploadUrl() + "fileDownload?path=" + path);
            msg.setData(metadata);
            return msg;
        } catch (Exception e) {
            logger.error("上传签字图片失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "上传失败", null);
        }
    }


    /**
     *@description:数据库插入附件信息
     * @param factory
     * @param file
     * @param userInfo 用户信息
     * @param path 服务器路径
     * @param receiptId 单据id
     * @param accessoryNo 附件类型
     */
    private BaseAccessoryMetadataBean addAccessoryData(SessionFactory factory,MultipartFile file,UserInfoBean userInfo,String path,String receiptId,String accessoryNo,
                                                       String userNo,String receiptType){
        BaseAccessoryMetadataMapper metadataMapper = factory.getMapper(BaseAccessoryMetadataMapper.class);
        BaseReceiptAccessoryMapper mapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
        BaseAccessoryMetadataBean metadata = new BaseAccessoryMetadataBean();
        String metadataId = Tools.getObjectId();
        metadata.setAccessoryPath(path);
        metadata.setAccessoryName(FilenameUtils.getName(file.getOriginalFilename()));
        metadata.setId(metadataId);
        metadata.setAccessorySize(file.getSize());
        metadata.setAccessoryType(file.getContentType());
        metadata.setCreateAccount(userInfo.getUserInfo().getAcount());
        metadata.setCreateName(userInfo.getUserInfo().getName());
        metadata.setSourceFlag("1");
        metadataMapper.insert(metadata);
        // 如果附件绑定了多个单据，则多个绑定的单据主键ID 用逗号分隔
        String[] receiptIdArray = receiptId.split(",", -1);
        for (String id : receiptIdArray){
            BaseReceiptAccessoryBean bean = new BaseReceiptAccessoryBean();
            bean.setId(Tools.getObjectId());
            bean.setMetadataId(metadataId);
            bean.setCreateAccount(userInfo.getUserInfo().getAcount());
            bean.setCreateName(userInfo.getUserInfo().getName());
            bean.setAccessoryName(FilenameUtils.getName(file.getOriginalFilename()));
            bean.setReceiptId(id);
            bean.setAccessoryNo(accessoryNo);
            bean.setUserNo(userNo);
            bean.setReceiptType(receiptType);
            mapper.insert(bean);
            metadata.setReceiptId(bean.getId());
        }
        return metadata;
    }
}
